#include "timeitem.h"
#include <QGridLayout>
#include <QLabel>
#include <QDateTime>
#include <QDebug>
#include <QMessageBox>

TimeItem::TimeItem(QWidget *parent)
    : QWidget{parent}
{
    onInit();
}

void TimeItem::onInit()
{
    QFont font = QFont("Consolas", 14);
    this->setFont(font);

    // 初始化UI界面和基本配置
    QGridLayout *mainLayout = new QGridLayout();
    this->setLayout(mainLayout);

    // 设置行间距和列间距
    mainLayout->setSpacing(10); // 设置控件之间的间隔为10

    mainLayout->setRowStretch(0, 1);

    // 获取当前时间和时间戳组件
    QLabel *curLabel = new QLabel(tr("当前时间:"));
    m_curTime = new QLineEdit();
    m_curTimestamp = new QLineEdit();
    // m_curTime->setEnabled(false);
    // m_curTimestamp->setEnabled(false);
    m_curBtn = new QPushButton("更新");
    mainLayout->addWidget(curLabel, 1, 0);
    mainLayout->addWidget(m_curTime, 1, 1);
    mainLayout->addWidget(m_curTimestamp, 1, 2);
    mainLayout->addWidget(m_curBtn, 1, 3);

    mainLayout->setRowStretch(2, 1);

    // 时间转时间戳
    QLabel *timestampLabel = new QLabel(tr("转时间戳:"));
    m_time = new QLineEdit();
    m_timestamp = new QLineEdit();
    // m_timestamp->setEnabled(false);
    m_btn = new QPushButton("转换");

    mainLayout->addWidget(new QLabel("输入时间"), 3, 1);
    mainLayout->addWidget(new QLabel("输出时间戳"), 3, 2);
    mainLayout->addWidget(timestampLabel, 4, 0);
    mainLayout->addWidget(m_time, 4, 1);
    mainLayout->addWidget(m_timestamp, 4, 2);
    mainLayout->addWidget(m_btn, 4, 3);


    mainLayout->setRowStretch(5, 1);

    // 时间戳转时间
    QLabel *timeLabel = new QLabel(tr("转时间:"));
    m_time2 = new QLineEdit();
    m_timestamp2 = new QLineEdit();
    // m_time2->setEnabled(false);
    m_btn2 = new QPushButton("转换");

    mainLayout->addWidget(new QLabel("输入时间戳"), 6, 1);
    mainLayout->addWidget(new QLabel("输出时间"), 6, 2);
    mainLayout->addWidget(timeLabel, 7, 0);
    mainLayout->addWidget(m_timestamp2, 7, 1);
    mainLayout->addWidget(m_time2, 7, 2);
    mainLayout->addWidget(m_btn2, 7, 3);

    // 设置行和列的伸展因子
    mainLayout->setRowStretch(8, 3); // 设置最后一行的伸展因子

    connect(m_curBtn, &QPushButton::clicked, this, &TimeItem::onGetCurrent);
    connect(m_btn, &QPushButton::clicked, this, &TimeItem::onTransTimestamp);
    connect(m_btn2, &QPushButton::clicked, this, &TimeItem::onTransTime);

    // 执行一次获取当前时间
    onGetCurrent();
    
}

void TimeItem::onGetCurrent()
{
    // 获取本地时间
    QDateTime curTime = QDateTime::currentDateTime();

    // 转字符串
    QString curTimeStr = curTime.toString("yyyy-MM-dd hh:mm:ss");

    // 转时间戳
    qint64 timestamp = curTime.toSecsSinceEpoch();

    this->m_curTime->setText(curTimeStr);
    this->m_curTimestamp->setText(QString("%1").arg(timestamp));

    //qDebug() << "当前时间戳：" << timestamp << " " << curTime.toUTC().toSecsSinceEpoch();
}

void TimeItem::onTransTimestamp()
{
    QString timeStr = m_time->text();
    if (timeStr.isEmpty()) {
        return;
    }

    if (timeStr.contains("/")) {
        timeStr = timeStr.replace("/", "-");
    }

    // 转成时间
    QDateTime time = QDateTime::fromString(timeStr, "yyyy-MM-dd hh:mm:ss");
    if (!time.isValid()) {
        time = QDateTime::fromString(timeStr, "yyyy-M-d hh:mm:ss");
    }

    if (!time.isValid()) {
        QMessageBox::critical(nullptr, "错误", "不支持的时间格式, 默认格式为:yyyy-MM-dd hh:mm:ss");
        return;
    }

    qint64 timestamp = time.toSecsSinceEpoch();

    this->m_timestamp->setText(QString("%1").arg(timestamp));

    qDebug() << timeStr << " " << time;
}

void TimeItem::onTransTime()
{
    QString timestampStr = m_timestamp2->text();
    if (timestampStr.isEmpty()) {
        return;
    }

    qlonglong timestamp = timestampStr.toLongLong(nullptr, 10);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
    QDateTime time = QDateTime::fromSecsSinceEpoch(timestamp);
#else
    QDateTime time = QDateTime::fromTime_t(timestamp);
#endif

    this->m_time2->setText(time.toString("yyyy-MM-dd hh:mm:ss"));
}
